package com.ejuornal.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ejuornal.bean.Admin;
import com.ejuornal.bean.vo.PermissionVO;
import com.ejuornal.service.AdminService;
import com.ejuornal.service.PermissionService;
import com.ejuornal.springutil.SpringUtil;
import com.ejuornal.util.Constants;
import com.ejuornal.util.MD5;
import com.ejuornal.util.String2Ints;

@Component("adminAction")
@Scope("prototype")
public class AdminAction extends BaseAction<Admin> {
	private static final long serialVersionUID = 1L;
	private AdminService adminService;
	private PermissionService permissionService;
    private Admin admin;  //椤甸潰浼犺繃鏉ョ殑admin
	private String  random;   //椤甸潰楠岃瘉鐮�
	private String  newPwd;   //鏂板瘑鐮�
	private int   adminId;  // 瓒呯骇缁欎竴鑸�閲嶇疆瀵嗙爜 
	private  String account;
	private int   current_record;
	private int   roleId;
	private String  adminIdStr;
	private List<PermissionVO> pv;
	private String oldAccount2;

    /**
	 * 绠＄悊鍛樼櫥闄嗛獙璇�
	 * @return
	 */
	public String login() {
		Admin  adminOld = adminService.login(admin);
		HttpSession session = getSession2();
		String  randomSessionStr = ((String) session.getAttribute("random"));
		if(randomSessionStr == null ){
			return INPUT;
		}
		String  randomSession =randomSessionStr.toLowerCase();
		String oldAccount=null;
		try {
			oldAccount = adminOld.getAccount();
		} catch (Exception e) {
			e.printStackTrace();
			super.getRequest().setAttribute("alert", "1");
			return INPUT;
		}
		String oldPsd=adminOld.getPassword();
		int roleId = adminOld.getRoleId();
		boolean  account=oldAccount.equals(admin.getAccount());
		boolean  psd=oldPsd.equals(MD5.MD5Encode(admin.getPassword()));
		boolean  rdm=randomSession.equals(random.toLowerCase());
		if(account && psd && rdm){			
			session.setAttribute(Constants.ADMIMID, adminOld.getAdminId());
		    session.setAttribute(Constants.ADMINSESSION, oldAccount);
		    session.setAttribute(Constants.ADMINSESSIONROLEID, Integer.toString(roleId));
			return SUCCESS;		
		}
		else {
			// 鏈夐敊璇椂鍊欏墠鍙�鎻愮ず鐢ㄦ埛瀵嗙爜閿欒
			super.getRequest().setAttribute("alert", "1");
			return   INPUT;
		}
	}
	
	public String findAdmin(){
		HttpSession session = getSession2();
		oldAccount2 = (String) session.getAttribute(Constants.ADMINSESSION);
		if(oldAccount2 == null || "".equals(oldAccount2)) {
			return "error_adminLogin";
		}
		return SUCCESS;
	}
	
	/**
	 * 娣诲姞绠＄悊鍛�
	 * @return
	 */
	public String addAdmin(){
		adminService =(AdminService) SpringUtil.getBean("adminService");
		if(admin.getAccount()!= null||admin.getPassword()!= null||admin.getAccount()!= ""||admin.getPassword()!= ""){
			adminService.add(admin);
			return  SUCCESS;
		}
		return  INPUT;
	}
	
	/**
	 * 鍒犻櫎绠＄悊鍛�
	 * @return
	 */
	public  String   deleteAdmin(){
		adminService=(AdminService) SpringUtil.getBean("adminService");		
		int [] adminIds =String2Ints.StringToInts(adminIdStr);
		admin = adminService.getAdminById(adminIds[0]);
		roleId = admin.getRoleId();
		adminService.delete(adminIds);
		return  SUCCESS;
	}
	/**
	 * 閲嶇疆瀵嗙爜
	 * @return
	 */
	public String resetPwd(){
		HttpSession session = getSession2();
		adminService=(AdminService) SpringUtil.getBean("adminService");
		Map<String, Object>	adminmMap= getSession();
		//Admin adminOld= (Admin) adminmMap.get("adminOld");
	//	admin.setAdminId(adminOld.getAdminId());
		// String account = (String) session.getAttribute(Constants.ADMINSESSION);
		    admin.setAdminId(getAdminIdBySession());
			adminService.resetPwd(admin, admin.getPassword());		//姝ゅ淇敼瀵嗙爜鏄惁闇�鍥炵瓟涓�釜闂
			return  SUCCESS;
		//	return  ERROR;		
	}
	
	public String resetPwdCommon(){
		adminService=(AdminService) SpringUtil.getBean("adminService");
		if(super.getAdminIdBySession() == -1) {
			return "error_adminLogin";
		}
		Admin admin=adminService.getAdminById(adminId);
		roleId = admin.getRoleId();
		adminService.resetPwd(admin, "123");		
		
		return  SUCCESS;
	}
	
	
	/**
	 * 鏌ユ壘瓒呯骇绠＄悊鍛樺垪琛紙鍒嗛〉锛�
	 * @return
	 */
	 public String listAdminByRoleId() {
	        String url ="admin/adminListAdmin";
	        roleId = Constants.SUPERADMIN_ROLE;
	        pages = adminService.findAllAdmin((page - 1) * 10, url, roleId);
	        return SUCCESS;
	    }
	 /**
		 * 鏌ユ壘鏈熷垔绠＄悊鍛樺垪琛紙鍒嗛〉锛�
		 * @return
		 */
	 public String listAdminByRoleId2() {
	        String url ="admin/adminListAdmin2";
	        roleId = Constants.PERADMIN_ROLE;
	        pages = adminService.findAllAdmin((page - 1) * 10, url, roleId);
	        return SUCCESS;
	    }
	 /**
		 * 鏌ユ壘鐢ㄦ埛绠＄悊鍛樺垪琛紙鍒嗛〉锛�
		 * @return
		 */
	 public String listAdminByRoleId3() {
	        String url ="admin/adminListAdmin3";
	        roleId = Constants.USERADMIN_ROLE;
	        pages = adminService.findAllAdmin((page - 1) * 10, url, roleId);
	        return SUCCESS;
	    }
	 /**
	  * 閫�嚭绯荤粺
	  * @return
	  */
	public  String goOut(){
		getRequest().getSession().invalidate();
		return  SUCCESS;
	}
	

	public String getRandom() {
		return random;
	}


	public void setRandom(String random) {
		this.random = random;
	}


	public Admin getAdmin() {
		return admin;
	}

	public void setAdmin(Admin adminNew) {
		this.admin = adminNew;
	}


	public AdminService getAdminService() {
		return adminService;
	}


	@Resource(name="adminService")
	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}


	public Admin getAdminNew() {
		return admin;
	}


	public void setAdminNew(Admin adminNew) {
		this.admin = adminNew;
	}

	public String getNewPwd() {
		return newPwd;
	}

	public void setNewPwd(String newPwd) {
		this.newPwd = newPwd;
	}

	public int getAdminId() {
		return adminId;
	}

	public void setAdminId(int adminId) {
		this.adminId = adminId;
	}

	public int getCurrent_record() {
		return current_record;
	}

	public void setCurrent_record(int current_record) {
		this.current_record = current_record;
	}

	public int getRoleId() {
		return roleId;
	}

	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}
	

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getAdminIdStr() {
		return adminIdStr;
	}

	public void setAdminIdStr(String adminIdStr) {
		this.adminIdStr = adminIdStr;
	}
	
	public PermissionService getPermissionService() {
        return permissionService;
    }

	@Resource(name="permissionService")
    public void setPermissionService(PermissionService permissionService) {
        this.permissionService = permissionService;
    }
	
	public List<PermissionVO> getPv() {
        return pv;
    }


    public void setPv(List<PermissionVO> pv) {
        this.pv = pv;
    }

	public String getOldAccount2() {
		return oldAccount2;
	}

	public void setOldAccount2(String oldAccount2) {
		this.oldAccount2 = oldAccount2;
	}
    
}
